package com.aforce.entities;

import javax.media.j3d.Appearance;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Group;
import javax.media.j3d.Material;
import javax.media.j3d.PointAttributes;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.aforce.behaviors.CollisionDetector;

import com.aforce.appearances.BoxAppearance;
import com.aforce.models.Box;

public class Block extends AbstractEntity
{
	final static BoxAppearance boxAppearances = new BoxAppearance();
	static Appearance wireFrame = boxAppearances.createAppearance(1);
	
	static int counter = 0;
	
	Appearance appearance = null;
	
	private ColoringAttributes shapeColoring;
	private PolygonAttributes shapePolygon;
	private PointAttributes shapePoint;
	private Appearance shapeAppearance;
	Box shape = null;
	
	double x_position;
	double y_position;
	double z_position;
	
	Group ogl_group;
	
	public Block(double x_position, double y_position, double z_position)
	{
		this.x_position = x_position;
		this.y_position = y_position;
		this.z_position = z_position;
		
		
		ogl_group = createGroup();
	}

	@Override
	public Group getTransformGroup()
	{
		return ogl_group;
	}
	
	private Group createGroup()
	{
		Vector3d pos = new Vector3d(x_position, y_position, z_position);
		double scale = 0.6;
		
		// Create a transform group node to scale and position the object.
		Transform3D t = new Transform3D();
		t.set(scale, pos);
		TransformGroup objTrans = new TransformGroup(t);

		// Create a simple shape leaf node and add it to the scene graph
		int xsize = (int)(Math.random()*10 % 3 + 1);
		int ysize = (int)(Math.random()*10 % 3 + 1);
		int zsize = (int)(Math.random()*10 % 3 + 1);
		shape = new Box(xsize, ysize, zsize);
		objTrans.addChild(shape);

		// Create a new ColoringAttributes object for the shape's
		// appearance and make it writable at runtime.
		
		/*
		
		
		Appearance app = shape.getAppearance();
		Material m = new Material(new Color3f(0.6f, 0.3f, 0.0f), new Color3f(0.6f, 0.3f, 0.0f), new Color3f(0.6f, 0.3f, 0.0f), new Color3f(0.6f, 0.3f, 0.0f), 100.0f);
		ColoringAttributes ca = new ColoringAttributes();
		ca.setColor(0.6f, 0.3f, 0.0f);
		app.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
		app.setColoringAttributes(ca);
		m.setLightingEnable(true);
		app.setMaterial(m);
		
		*/

		
		shapeAppearance = boxAppearances.createAppearance(counter);
		shapeColoring = shapeAppearance.getColoringAttributes();
		shapePolygon = shapeAppearance.getPolygonAttributes();
		shapePoint = shapeAppearance.getPointAttributes();
		shape.setAppearance(shapeAppearance);
		
		counter++;
		if (counter == 13)
			counter = 0;
		
		// Create a new Behavior object that will perform the collision
		// detection on the specified object, and add it into
		// the scene graph.
		CollisionDetector cd = new CollisionDetector(shape);
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
		cd.setSchedulingBounds(bounds);

		// Add the behavior to the scene graph
		objTrans.addChild(cd);

		return objTrans;
	}
	
	public void setWireFrame(boolean b){
		if (b){
			Color3f objColor = new Color3f(0.5f, 0.0f, 0.2f);
			ColoringAttributes ca = new ColoringAttributes();
			ca.setColor(objColor);

			// Set up the polygon attributes
			PolygonAttributes pa = new PolygonAttributes();
			pa.setPolygonMode(pa.POLYGON_LINE);
			pa.setCullFace(pa.CULL_NONE);
			
			
			
			shapeAppearance.setColoringAttributes(ca);
			shapeAppearance.setPolygonAttributes(pa);
			
		}else{
			shapeAppearance.setColoringAttributes(shapeColoring);
			shapeAppearance.setPolygonAttributes(shapePolygon);
			shapeAppearance.setPointAttributes(shapePoint);
		}
	}
}


